//
//  SectionsViewController.m
//  WE
//
//  Created by iMacOne on 15/11/18.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "SectionsViewController.h"
#import "Head.h"
#import "DoctorViewController.h"
#import "MoreTeamViewController.h"
#import "MoreAdviViewController.h"
#import "RelatedAdviViewController.h"
#import "MoreRelatedAdviCell.h"
#import "FreeAdviViewController.h"
#import "MoreAdviViewController.h"
#import "DiseaseViewController.h"
#import "HealthAdviDetailControl.h"
#import "EMMallSectionView.h"
@interface SectionsViewController ()
//@property (nonatomic, strong)UIButton * regiBtn;
@property (nonatomic, strong)UIView * bottomView;


@end

@implementation SectionsViewController



- (void)viewWillAppear:(BOOL)animated
{
   self.navigationController.navigationBar.hidden = NO;

}
//
//- (void)viewDidAppear:(BOOL)animated{
//    self.navigationController.navigationBarHidden = YES;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setExtraCellLineHidden:self.tableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([self.sectionsTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.sectionsTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.sectionsTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.sectionsTableView setLayoutMargins:UIEdgeInsetsZero];
    }

    self.navigationItem.title = self.titleStr;
    self.navigationController.navigationBar.hidden = NO;

    self.view.backgroundColor = [UIColor whiteColor];
    //self.navigationItem.title = self.titleStr;
    
    self.segmentControl  = [[UISegmentedControl alloc] initWithItems:@[@"就医推荐",@"健康咨询"]];
    _segmentControl.frame = CGRectMake(margin_30 , 84 , WindowWidth - margin_30 * Width*2, margin_34);
    _segmentControl.selectedSegmentIndex = 0;
    [_segmentControl setTintColor:[UIColor blueColor]];
    [_segmentControl addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentControl];
    
    [ToolUtil createLine:139 superView:self.view lift:0 width:WindowWidth hight:1 color:GRAYCOLOR];
    
    [self.view addSubview:self.sectionsTableView];
    [self creatBottomBtn];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([self.sectionsTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.sectionsTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.sectionsTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.sectionsTableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
}
- (void)creatBottomBtn
{
    NSArray * arr = @[@"预约挂号",@"在线咨询"];
    NSArray * arr1 = @[@"iconfont-guahao",@"iconfont-zixun"];
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, WindowHeight - 49, WindowWidth, 49)];
    [ToolUtil createLine:0 superView:_bottomView lift:0 width:_bottomView.frame.size.width hight:1 color:GRAYCOLOR];
    
    [ToolUtil createVerticalLine:0 superView:_bottomView lift:WindowWidth / 2 - 1 height:_bottomView.frame.size.height];
    for (int i = 0; i < 2; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i *(WindowWidth / 2 ) ,1, (WindowWidth / 2 -1), 48);
        [btn addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 300 + i;
        btn.backgroundColor = [UIColor whiteColor];
        
        UIImageView * aImageView = [[UIImageView alloc] initWithFrame:CGRectMake( ((WindowWidth / 2) - 94)/2, 15, margin_20, margin_20)];
        aImageView.layer.masksToBounds = YES;
        aImageView.layer.cornerRadius = 10;
        if (i == 0) {
            
            aImageView.backgroundColor = [UIColor colorWithRed:0.4159 green:0.5049 blue:1.0 alpha:1.0];
        }
        aImageView.image = [UIImage imageNamed:arr1[i]];
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(aImageView.frame) + margin_10, 15, margin_64, margin_20)];
        lbl.font = FONT(14);
        lbl.text = arr[i];
        [btn addSubview:lbl];
        [btn addSubview:aImageView];
        [self.bottomView addSubview:btn];
    }
    [self.view addSubview:self.bottomView];
}

- (void)clickbtn:(UIButton *)btn
{
    if (btn.tag == 300) {
        NSLog(@"挂号");
        MoreAdviViewController * moreAdviVC = [[MoreAdviViewController alloc] init];
        [self.navigationController pushViewController:moreAdviVC animated:YES];
    }else if (btn.tag == 301){
        MoreAdviViewController * moreAdviVC = [[MoreAdviViewController alloc] init];
        [self.navigationController pushViewController:moreAdviVC animated:YES];

        NSLog(@"咨询");
    }
    
}
- (void)doClickBtn:(NSInteger)number cell:(UITableViewCell *)cell
{
    NSLog(@"%ld",number);
    if ([cell isKindOfClass:[DiseasesTableViewCell class]]) {
        if (number == 300) {
            [self.navigationController pushViewController:[[CityViewController alloc] init] animated:YES];
        }else{
            DiseaseViewController * disVC = [[DiseaseViewController alloc] init];
            disVC.string = @"霍纳";
            [self.navigationController pushViewController:disVC animated:YES];
        }
    }else if ([cell isKindOfClass:[TeamTableViewCell class]]){
        if (number == 100) {
            NSLog(@"kkk");
            MoreTeamViewController * moreTeamVC = [[MoreTeamViewController alloc] init];
            [self.navigationController pushViewController:moreTeamVC animated:YES];
            
        }else{
        TeamViewController * teamVC = [[TeamViewController alloc] init];
            
        [self.navigationController pushViewController:teamVC animated:YES];
        }
    }else if ([cell isKindOfClass:[AdvisoryCell class]]){
        if (number == 100) {
            MoreAdviViewController * moreAdiVC = [[MoreAdviViewController alloc] init];
            [self.navigationController pushViewController:moreAdiVC animated:YES];
        }else{
            DoctorViewController * doctorVC = [[DoctorViewController alloc] init];
            [self.navigationController pushViewController:doctorVC animated:YES];
        }
    }else if ([cell isKindOfClass:[RelatedAdvisoryCell class]]){
        RelatedAdviViewController * relaAdvVC = [[RelatedAdviViewController alloc] init];
        [self.navigationController pushViewController:relaAdvVC animated:YES];
    }

    
}
- (void)doClick:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        [self.view addSubview:self.sectionsTableView];
        self.sectionsTableView.hidden = NO;
        self.tableView.hidden = YES;
        self.bottomView.hidden = NO;
    }else if (segment.selectedSegmentIndex == 1){
        [self.view addSubview:self.tableView];
        self.tableView.hidden = NO;
        self.sectionsTableView.hidden = YES;
        self.bottomView.hidden = YES;
    }
}
- (UITableView *)sectionsTableView
{
    if (_sectionsTableView == nil) {
        self.sectionsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, margin_140,WindowWidth , WindowHeight - margin_140 - 49) style:UITableViewStylePlain];
        _sectionsTableView.dataSource = self;
        _sectionsTableView.delegate = self;
        _sectionsTableView.showsVerticalScrollIndicator = NO;
    }
    return _sectionsTableView;
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, margin_140,WindowWidth , WindowHeight - margin_140) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (tableView == self.sectionsTableView) {
        if (section != 4 || section !=5) {
            EMMallSectionView *sectionView =  [EMMallSectionView showWithName:nil];
            sectionView.tableView = self.sectionsTableView;
            sectionView.section = section;
            sectionView.sign = @"2";
            return sectionView;
        }
        return nil;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView == self.sectionsTableView) {
        if (section == 4 || section == 5) {
            return 0.001f;
        }
        return 10;
    }
    return 0.001f;
}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.sectionsTableView) {
        if (section == 5) {
            return 2;
        }
        return 1;
    }
    return 9;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.sectionsTableView) {
        
        if (indexPath.section == 0) {
            
            static NSString * cellIdentifier = @"cell";
            DiseasesTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell == nil) {
                cell = [[DiseasesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                cell.delegate = self;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
        }else if (indexPath.section == 1){
            static NSString * cellIdentifier = @"cell1";
            TeamTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

            if (cell == nil) {
                cell = [[TeamTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                cell.delegate = self;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
        }else if (indexPath.section == 2){
            AdvisoryCell * cell = [[AdvisoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            cell.delegate = self;
            return cell;
        }else if (indexPath.section == 3){
            FreeAdvisoryCell * cell = [[FreeAdvisoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.text = @"免费咨询";
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.textLabel.font = FONT(14);

            return cell;
        }else if (indexPath.section == 4){
            RelatedAdvisoryCell * cell = [[RelatedAdvisoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            
            cell.delegate = self;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
        }else if (indexPath.section == 5){
        static NSString * cellIdentifier = @"cell2";
        MoreRelatedAdviCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[MoreRelatedAdviCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            //        cell.delegate = self;
        }
        return cell;
       }
    }
    
    static NSString * cellIdentifier = @"cellOne";
    HealthCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HealthCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 3) {
        FreeAdviViewController * freeVC = [[FreeAdviViewController alloc] init];
        
        [self.navigationController pushViewController:freeVC animated:YES];
    }
    if (tableView == self.tableView) {
        [self.navigationController pushViewController:[[HealthAdviDetailControl alloc] init] animated:YES];
    }
   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.sectionsTableView) {
        return 6;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.sectionsTableView) {
        
        if (indexPath.section == 0) {
            return 110;
        }else if (indexPath.section == 1){
            return 150;
        }else if (indexPath.section == 3){
            return 85;
        }else if (indexPath.section == 4){
            return 34;
        }else if (indexPath.section == 5){
            return 180;
        }
        return 140;
    }
    return 84;
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat sectionHeaderHeight = 10;//设置你footer高度
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *coverView = [UIView new];
    tableView.tableFooterView = coverView;
}


@end
